import {onMounted, ref} from "vue";
import axios from "@/utils/axios";
import {message} from "ant-design-vue";

class Formal{
    id: number | undefined
    mpId: number | undefined
    mpUuid: number | undefined
    name: string | undefined
    gl1: string | undefined
    gl1Range: number| undefined
    gl1Design: number | undefined
    gl1Type: number | undefined

    gl2: string | undefined
    gl2Range: number | undefined
    gl2Design: number | undefined
    gl2Type: number | undefined

    gl3: string | undefined
    gl3Range: number | undefined
    gl3Design: number | undefined
    gl3Type: number | undefined

    gl4: string | undefined
    gl4Range: number | undefined
    gl4Design: number | undefined
    gl4Type: number | undefined

    gl5: string | undefined
    gl5Range: number | undefined
    gl5Design: number | undefined
    gl5Type: number | undefined

    gl6: string | undefined
    gl6Range: number | undefined
    gl6Design: number | undefined
    gl6Type: number | undefined

    cement: string |undefined
    cementRange: number | undefined
    cementDesign: number |undefined

    water: string | undefined
    waterRange: number | undefined
    waterDesign: number | undefined

    additive: string | undefined
    additiveRange: number | undefined
    additiveDesign: number | undefined

    flyash: string | undefined
    flyashRange: number | undefined
    flyashDesign: number | undefined
    coarseAggregatePlan: number | undefined
    fineAggregatePlan: number | undefined
    createdTime: string | undefined
}
class GlType{
    typeKey: number | undefined
    typeValue: string | undefined

    constructor(typeKey:number,typeValue:string) {
        this.typeKey = typeKey;
        this.typeValue = typeValue;
    }
}

export default () => {
    const selformal=ref(-1)
    const formal: any = ref(new Formal());
    const FormalList:any = ref({});
    const glTypeList = ref([
        new GlType(0,"未选择"),
        new GlType(1,"粗骨料"),
        new GlType(2,"细骨料"),
    ])

    // 新增指定拌合站标准
    let addFormal = ()=>{
        if(FormalList.value.mpId > 0){
            axios
            .post("/mix/formal/add",formal.value)
            .then((res:any) => {
                if(res.success){
                    message.success("更新成功")
                    getFormalByMpId(FormalList.value.mpId)
                }else{
                    message.error("更新失败,请确认验证码及标准是否设置正确")
                }
            });
        }else{
            message.error("请先选择拌合站")
        }
    }

    // 删除指定拌合站配方
    let delFormal = (selformal:number)=>{
        if (FormalList.value.mpId > 0) {
            axios
                .post("/mix/formal/del", {
                    id: selformal,
                    mpId: FormalList.value.mpId ,
                    mpUuid:formal.value.mpUuid
                })
                .then((res: any) => {
                    if(res.success){
                        message.success("删除成功")
                        getFormalByMpId(FormalList.value.mpId)
                    }else{
                        message.error("删除失败,请确认验证码及标准是否设置正确")
                    }
                });
        }else{
            message.error("请先选择拌合站")
        }
    }

    // 获取指定拌合站配方详情
    let editFormal = (id:any)=>{
        axios
            .post("/mix/formal/getOneByMpIdAndFId",{
                id: id,
                mpId: FormalList.value.mpId ,
            })
            .then((res: any) => {
                if(res.success){
                    if (res.data != null) {
                        formal.value = res.data;
                    } else{
                        formal.value = new Formal();
                        formal.value.mpId = id;
                    }

                }else{
                    message.error("获取标准失败")
                }

            });
    }

    // 获取指定拌合站配方
    let getFormalByMpId = (id:any)=>{
        axios
            .post("/mix/formal/getAllFormalByMpId",{
                mpId: id,
            })
            .then((res: any) => {
                FormalList.value = {}
                FormalList.value.mpId = id;
                formal.value.mpId = id;
                FormalList.value.table = []
                for (let i = 0; i < res.data.length; i++) {
                   
                    FormalList.value.table.push({
                        index: i + 1,
                        name: res.data[i].name,
                        key:res.data[i].id
                    })
                }

            });
    }

    onMounted(()=>{
        // getFormalByMpId()
    })
    return {
        formal,
        glTypeList,
        addFormal,
        delFormal,
        getFormalByMpId,
        editFormal,
        FormalList
    }
}